package com.sciencep.pro.admin.system.controller;

import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.exception.CustomException;
import com.sciencep.pro.core.domain.entity.TaskLog;
import com.sciencep.pro.core.service.ITaskService;
import com.taobao.api.ApiException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;


@RestController
@Slf4j
@EnableScheduling
@RequestMapping("/task")
public class TaskController {

    @Autowired
    ITaskService taskService;

    @GetMapping(value = "lastConcatUser")
    @ApiOperation(value = "最近更新人员的信息")
    public ResponseResult<Object> lastConcatUser() {
        return new ResponseResult<>().success(taskService.lastConcatUser());
    }

    @GetMapping(value = "lastConcatLog")
    @ApiOperation(value = "最近更新部门的信息")
    public ResponseResult<List<TaskLog>> lastConcatLog() {
        return new ResponseResult<List<TaskLog>>().success(taskService.lastConcatLog());
    }

    @GetMapping(value = "concatUser")
    @ApiOperation(value = "更新人员(1.更新部门 2.更新用户和部门关联表 3.更新角色和用户关联表 4.更新离职员工)")
    public ResponseResult<List<TaskLog>> concatUser() {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatUser());
    }

    @GetMapping(value = "concatLog")
    @ApiOperation(value = "更新部门(1.更新考勤 2.更新用户请假信息 3.更新用户日志信息)")
    public ResponseResult<List<TaskLog>> concatLog() {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatLog());
    }

    @GetMapping(value = "concatLogNew")
    @ApiOperation(value = "更新部门(1.更新考勤 2.更新用户请假信息 3.更新用户日志信息)")
    public ResponseResult<List<TaskLog>> concatLogNew(@RequestParam(value = "time") String time) {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatLogNew(time));
    }

    @GetMapping(value = "getDeptList")
    @ApiOperation(value = "更新部门(每天22点执行一次)")
    @Scheduled(cron = "0 0 22 * * ?")
    public ResponseResult<Object> getDeptList() {
        return taskService.getDeptList();
    }

    @GetMapping(value = "getUserList")
    @ApiOperation(value = "更新用户和部门关联表(每天22点执行一次)")
    @Scheduled(cron = "0 0 22 * * ?")
    public ResponseResult<Object> getUserList() {
        return taskService.getUserList();
    }


    @GetMapping(value = "getUserRole")
    @ApiOperation(value = "更新角色和用户关联表(每天22点执行一次)")
    @Scheduled(cron = "0 0 22 * * ?")
    public ResponseResult<Object> getUserRole() {
        return taskService.getUserRole();
    }


    @GetMapping(value = "getResignUser")
    @ApiOperation(value = "更新离职员工(每天23点执行一次)")
    @Scheduled(cron = "0 0 23 * * ?")
    public ResponseResult<Object> getResignUser() {
        return taskService.getResignUser();
    }

    @GetMapping(value = "getAttendanceList")
    @ApiOperation(value = "更新考勤(每天23:55执行一次)")
    @Scheduled(cron = "0 55 23 * * ?")
    public ResponseResult<Object> getAttendanceList() {
        return taskService.getAttendanceList(null);
    }

    @GetMapping(value = "getLeaveList")
    @ApiOperation(value = "更新用户请假信息(每天23:55执行一次)")
    @Scheduled(cron = "0 55 23 * * ?")
    public ResponseResult<Object> getLeaveList() {
        return taskService.getLeaveList();
    }

    @GetMapping(value = "getWorkDailyList")
    @ApiOperation(value = "更新用户日志信息(每天23:55执行一次)")
    @Scheduled(cron = "0 55 23 * * ?")
    public ResponseResult<Object> getWorkDailyList() {
        return taskService.getWorkDailyList(null);
    }


    @ApiOperation(value = "定时获取审批结果")
    @GetMapping(value = "getProcessingResult")
    @Scheduled(cron = "0 */1 * * * ?")
    public void getProcessingResult() {
        taskService.getProcessingResult();
    }

    @GetMapping(value = "getCalendar")
    @ApiOperation(value = "获取日历(每年的1月1号凌晨1点)")
    @Scheduled(cron = "0 0 1 1 1 ?")
    public ResponseResult<Object> getCalendar() throws ParseException {
        return taskService.getCalendar();
    }


    @GetMapping(value = "getOtherDeptList")
    @ApiOperation(value = "更新外包人员部门信息(每天22:30执行一次)")
    @Scheduled(cron = "0 30 22 * * ?")
    public ResponseResult<Object> getOtherDeptList() throws CustomException, ApiException {
        return taskService.getOtherDeptList();
    }

    @GetMapping(value = "getOtherUserList")
    @ApiOperation(value = "更新外包用户和部门关联表(每天22点30执行一次)")
    @Scheduled(cron = "0 30 22 * * ?")
    public ResponseResult<Object> getOtherUserList() throws CustomException, ApiException {
        return taskService.getOtherUserList();
    }

    @GetMapping(value = "getOtherResignUser")
    @ApiOperation(value = "更新外包离职员工(每天23点30执行一次)")
    @Scheduled(cron = "0 30 23 * * ?")
    public ResponseResult<Object> getOtherResignUser() throws CustomException, ApiException {
        return taskService.getOtherResignUser();
    }

    @GetMapping(value = "getOtherAttendanceList")
    @ApiOperation(value = "更新外包考勤(每天23:50执行一次)")
    @Scheduled(cron = "0 50 23 * * ?")
    public ResponseResult<Object> getOtherAttendanceList() throws CustomException, ApiException {
        return taskService.getOtherAttendanceList(null);
    }

    @GetMapping(value = "getOtherLeaveList")
    @ApiOperation(value = "更新外包用户请假信息(每天23:50执行一次)")
    @Scheduled(cron = "0 50 23 * * ?")
    public ResponseResult<Object> getOtherLeaveList() throws CustomException, ApiException {
        return taskService.getOtherLeaveList();
    }

    @GetMapping(value = "getOtherWorkDailyList")
    @ApiOperation(value = "更新外包用户日志信息(每天23:50执行一次)")
    @Scheduled(cron = "0 50 23 * * ?")
    public ResponseResult<Object> getOtherWorkDailyList() throws CustomException, ApiException {
        return taskService.getOtherWorkDailyList(null);
    }

    @GetMapping(value = "concatOtherLog")
    @ApiOperation(value = "更新外包信息(1.人员 2.部门 3.离职 4.请假 5.考勤 6.更新用户日志信息)")
    public ResponseResult<List<TaskLog>> concatOtherLog() throws CustomException, ApiException {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatOtherLog());
    }

    @GetMapping(value = "concatOtherLogNew")
    @ApiOperation(value = "更新外包信息(1.人员 2.部门 3.离职 4.请假 5.考勤 6.更新用户日志信息)")
    public ResponseResult<List<TaskLog>> concatOtherLogNew(@RequestParam(value = "time") String time) throws CustomException, ApiException {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatOtherLogNew(time));
    }

    @GetMapping(value = "concatLastOtherLog")
    @ApiOperation(value = "更新外包信息(1.人员 2.部门 3.离职 4.请假 5.考勤 6.更新用户日志信息)")
    public ResponseResult<List<TaskLog>> concatLastOtherLog() {
        return new ResponseResult<List<TaskLog>>().success(taskService.concatLastOtherLog());
    }

}
